/*
 * Created on Jul 17, 2005
 *
 */
package org.mushroomdb.interceptor;

import java.util.Iterator;

import org.mushroomdb.catalog.Column;
import org.mushroomdb.catalog.EncodeableRegister;
import org.mushroomdb.catalog.Register;
import org.mushroomdb.catalog.Table;


/**
 * @author Matu
 *
 */
public class InterceptedTable implements Table {
	private Interceptor interceptor;
	private Table table;

	/**
	 * Constructor default
	 */
	public InterceptedTable(Interceptor interceptor, Table table) {
		this.interceptor = interceptor;
		this.table = table;
	}

	/**
	 * @see org.mushroomdb.catalog.Table#getName()
	 */
	public String getName() {
		return this.table.getName();
	}

	/**
	 * @see org.mushroomdb.catalog.Table#setName(java.lang.String)
	 */
	public void setName(String name) {
		this.table.setName(name);
	}

	/**
	 * @see org.mushroomdb.catalog.Table#getColumns()
	 */
	public Iterator getColumns() {
		return this.table.getColumns();
	}

	/**
	 * @see org.mushroomdb.catalog.Table#addColumn(com.mdlm.rdbms.catalog.Column)
	 */
	public void addColumn(Column column) {
		this.table.addColumn(column);
	}

	/**
	 * @see org.mushroomdb.catalog.Table#getColumn(java.lang.String)
	 */
	public Column getColumn(String name) {
		return this.table.getColumn(name);
	}

	/**
	 * @see org.mushroomdb.catalog.Table#getRegisters()
	 */
	public Iterator getRegisters() {
		final Iterator registers = this.table.getRegisters();
		
		/** Se agrega el interceptor de registro a cada registro devuelto */
		return new Iterator() {
			public void remove() {
				registers.remove();
			}

			public boolean hasNext() {
				return registers.hasNext();
			}

			public Object next() {
				Register register = (Register) registers.next();
				return new InterceptedRegister(
						InterceptedTable.this.interceptor,
						register,
						InterceptedTable.this.table);
			}
		};
	}

	/**
	 * @see org.mushroomdb.catalog.Table#getPages()
	 */
	public Iterator getPages() {
		return this.table.getPages();
	}

	/**
	 * @see org.mushroomdb.catalog.Table#insertRegister(com.mdlm.rdbms.catalog.EncodeableRegister)
	 */
	public void insertRegister(EncodeableRegister register) {
		this.interceptor.beforeInsert(register,this.table);
		this.table.insertRegister(register);
		this.interceptor.afterInsert(register,this.table);
	}

	/**
	 * @see org.mushroomdb.catalog.Table#deleteRegister(org.mushroomdb.catalog.Register)
	 */
	public boolean deleteRegister(Register register) {
		this.interceptor.beforeDelete(register,this.table);
		boolean deleted = this.table.deleteRegister(register);
		this.interceptor.afterDelete(register,this.table);
		return deleted;
	}
	
	public boolean equals(Object arg0) {
		return this.table.equals(arg0);
	}
	
	/**
	 * toString
	 */
	public String toString() {
		return this.table.toString();
	}

	/**
	 * @see org.mushroomdb.catalog.Table#getRegisterCount()
	 */
	public long getRegisterCount() {
		return this.table.getRegisterCount();
	}
}
